package com.hiinfo.mvpdemo.weather;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hiinfo.mvpdemo.R;
import com.hiinfo.mvpdemo.base.bean.BaseObjectBean;
import com.hiinfo.mvpdemo.base.component.BaseMvpActivity;

import butterknife.BindView;
import butterknife.OnClick;

public class WeatherActivity extends BaseMvpActivity<WeatherPresent> implements WeatherContract.View {

    private static final String TAG = WeatherActivity.class.getSimpleName();

    @BindView(R.id.vg_weather_info)
    RelativeLayout mWeatherInfoLayout;
    @BindView(R.id.city_value_tv)
    TextView mCityValueTv;
    @BindView(R.id.city_id_value_tv)
    TextView mCityIdValueTv;
    @BindView(R.id.temp1_value_tv)
    TextView mTemp1ValueTv;
    @BindView(R.id.temp2_value_tv)
    TextView mTemp2ValueTv;
    @BindView(R.id.weather_value_tv)
    TextView mWeatherValueTv;
    @BindView(R.id.time_value_tv)
    TextView mTimeValueTv;
    @BindView(R.id.btn_query_weather)
    Button mQueryWeatherBt;

    @BindView(R.id.pb_progress)
    ProgressBar mProgressBar;
    @BindView(R.id.query_failure_tv)
    TextView mQueryFailureTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_query_weather;
    }

    @Override
    public void init() {
        mPresenter = new WeatherPresent();
        mPresenter.attachView(this);
    }

    @Override
    public void showLoading() {
        if (mProgressBar != null) {
            mProgressBar.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void hideLoading() {
        if (mProgressBar != null) {
            mProgressBar.setVisibility(View.GONE);
        }
    }

    @Override
    public void onError(Throwable throwable) {
        mQueryWeatherBt.setEnabled(true);
        mQueryFailureTv.setVisibility(View.VISIBLE);
    }

    @Override
    public void startLoading() {
        mQueryWeatherBt.setEnabled(false);
        mWeatherInfoLayout.setVisibility(View.GONE);
        mQueryFailureTv.setVisibility(View.GONE);
    }

    @Override
    public void onSuccess(BaseObjectBean<WeatherInfo> bean) {
        Log.d(TAG, "onSuccess: ");
        WeatherInfo weatherInfo = bean.getWeatherinfo();
        mCityValueTv.setText(weatherInfo.getCity());
        mCityIdValueTv.setText(weatherInfo.getCityid());
        mTemp1ValueTv.setText(weatherInfo.getTemp1());
        mTemp2ValueTv.setText(weatherInfo.getTemp2());
        mWeatherValueTv.setText(weatherInfo.getWeather());
        mTimeValueTv.setText(weatherInfo.getPtime());
    }

    @Override
    public void endLoading() {
        mQueryWeatherBt.setEnabled(true);
        mWeatherInfoLayout.setVisibility(View.VISIBLE);
    }

    @OnClick(R.id.btn_query_weather)
    public void onViewClicked() {
        mPresenter.queryWeather();
    }
}